#!/usr/bin/perl -w

use strict;

open(CD_REPORT, ">_format.rpt");

my ($album, $artist, $price); 
my $total = 0;

format CD_REPORT =
  @<<<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<< $@###.##
  dotize(17, $album), dotize(16, $artist), $price
.

while (<STDIN>) {
   chomp;
   ($album, $artist, $price) = split(/!/);

   $album = "" unless defined($album);
   $artist = "" unless defined($artist);
   $price = 0 unless defined($price);
   $total = $total + $price;
   write(CD_REPORT);
}

close (CD_REPORT);

sub dotize {
    my ($width, $string) = @_;
    if (length($string) > $width) {
        return(substr($string, 0, $width - 3) . "...");
    } else {
        return($string);
    }
}

